//
//  RecordHomeCell.m
//  TourPlay
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "RecordHomeCell.h"

#define WIDTH 8

@interface RecordHomeCell ()

@property (nonatomic, retain) UIImageView *userHeadImageV;
@property (nonatomic, retain) UILabel *userName;
@property (nonatomic, retain) UILabel *userText;
@property (nonatomic, retain) UIImageView *userImageV;
@property (nonatomic, retain) UILabel *activityName;
@property (nonatomic, retain) UILabel *timeL;
@property (nonatomic, retain) UILabel *sourceLabel;
@property (nonatomic, retain) UIView *viewBack;

@end

@implementation RecordHomeCell
- (void)dealloc
{
    [_userHeadImageV release];
    [_userName release];
    [_userText release];
    [_userImageV release];
    [_activityName release];
    [_timeL release];
    [_recordMod release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    self.viewBack = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, WIDTH, SCREEN_WIDTH - WIDTH * 2, (SCREEN_HEIGHT - 64 - 49) / 2.f - WIDTH)];
    self.viewBack.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9];
    // 圆角
    self.viewBack.layer.cornerRadius = 4.f;
    [self.contentView addSubview:self.viewBack];
    [_viewBack release];
    
    
    
    self.userHeadImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10  * AUTOSCREEN_WIDTH, 13 * AUTOSCREEN_HEIGHT, 45 * AUTOSCREEN_WIDTH, 45 * AUTOSCREEN_HEIGHT)];
    self.userHeadImageV.backgroundColor = [UIColor clearColor];
    // 裁掉边框
    self.userHeadImageV.layer.masksToBounds = YES;
    self.userHeadImageV.contentMode = UIViewContentModeScaleAspectFill;
    self.userHeadImageV.layer.cornerRadius = self.userHeadImageV.frame.size.height / 2.f;
    [self.viewBack addSubview:self.userHeadImageV];
    [_userImageV release];
    
    self.userName = [[UILabel alloc] initWithFrame:CGRectMake(self.userHeadImageV.frame.origin.x + self.userHeadImageV.frame.size.width + 10 * AUTOSCREEN_WIDTH, self.userHeadImageV.frame.origin.y - 5 * AUTOSCREEN_HEIGHT, self.viewBack.frame.size.width - (self.userHeadImageV.frame.origin.x + self.userHeadImageV.frame.size.width + 25 * AUTOSCREEN_WIDTH), self.userHeadImageV.frame.size.height * (2 / 3.f))];
//    self.userName.backgroundColor = [UIColor orangeColor];
    self.userName.textColor = [UIColor colorWithRed:0.35 green:0.43 blue:0.6 alpha:1];
    self.userName.font = [UIFont systemFontOfSize:15.f];;
    [self.viewBack addSubview:self.userName];
    [_userName release];
    
    self.userText = [[UILabel alloc] initWithFrame:CGRectMake(self.userName.frame.origin.x, self.userName.frame.origin.y + self.userName.frame.size.height + 2 * AUTOSCREEN_HEIGHT, self.userName.frame.size.width, self.userName.frame.size.height)];
//    self.userText.backgroundColor = [UIColor brownColor];
    self.userText.textColor = [UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1];
    self.userText.numberOfLines = 0;
    self.userText.font = [UIFont systemFontOfSize:16.f * AUTOSCREEN_WIDTH];
    [self.viewBack addSubview:self.userText];
    [_userText release];
    
    self.userImageV = [[UIImageView alloc] initWithFrame:CGRectMake(self.userText.frame.origin.x, self.userText.frame.origin.y + self.userText.frame.size.height + 5, 200 * AUTOSCREEN_WIDTH, 140 * AUTOSCREEN_HEIGHT)];
//    self.userImageV.backgroundColor = [UIColor orangeColor];
    self.userImageV.layer.masksToBounds = YES;
    self.userImageV.layer.cornerRadius = 4.f;
    self.userImageV.contentMode = UIViewContentModeScaleAspectFill;
    [self.viewBack addSubview:self.userImageV];
    [_userText release];

    self.sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.userImageV.frame.origin.x, self.userImageV.frame.origin.y + self.userImageV.frame.size.height + 5, 50, 20)];
    self.sourceLabel.text = @"来自于";
//    sourceLabel.backgroundColor = [UIColor orangeColor];
    self.sourceLabel.font = [UIFont systemFontOfSize:14.f];
    self.sourceLabel.textColor = [UIColor colorWithRed:0.73 green:0.7 blue:0.71 alpha:1];
    [self.viewBack addSubview:self.sourceLabel];
    [_sourceLabel release];
    
    
    self.activityName = [[UILabel alloc] initWithFrame:CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.frame.size.width, self.sourceLabel.frame.origin.y, self.userName.frame.size.width - self.sourceLabel.frame.size.width, self.sourceLabel.frame.size.height)];
//    self.activityName.backgroundColor = [UIColor orangeColor];
    self.activityName.font = self.sourceLabel.font;
    self.activityName.textColor = self.userName.textColor;
    [self.viewBack addSubview:self.activityName];
    [_activityName release];
    
    self.timeL = [[UILabel alloc] initWithFrame:CGRectMake(self.userName.frame.origin.x, self.activityName.frame.origin.y + self.activityName.frame.size.height + 5, self.userName.frame.size.width, self.activityName.frame.size.height)];
//    self.timeL.backgroundColor = [UIColor orangeColor];
    self.timeL.font = self.sourceLabel.font;
    self.timeL.textColor = self.sourceLabel.textColor;
    [self.viewBack addSubview:self.timeL];
    [_timeL release];
    
    
    [self addColorChangedBlock:^{
        
        self.viewBack.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        self.viewBack.normalBackgroundColor = [ColorSingleton shareSingleton].cellColor;
    }];
}
- (void)setRecordMod:(RecordMod *)recordMod
{
    if (_recordMod != recordMod) {
        [_recordMod release];
        _recordMod = [recordMod retain];
    }

    [self getFromWithRecordMod:self.recordMod];

}

- (void)getFromWithRecordMod:(RecordMod *)recordMod
{
    NSURL *url = [NSURL URLWithString:self.recordMod.userPic];
    [self.userHeadImageV sd_setImageWithURL:url placeholderImage:PICHOLDEER];
    
    self.userName.text = self.recordMod.userName;
    
    self.userText.text = self.recordMod.comment;
    
    
    CGRect textRect = self.userText.frame;
    textRect.size.height = [AppTools heightForContent:self.recordMod.comment width:self.userText.frame.size.width font:16.f * AUTOSCREEN_WIDTH]  * AUTOSCREEN_HEIGHT;
    self.userText.frame = textRect;
    
    if (0 != self.userText.text.length) {
        
        // 调整行间距
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.userText.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3.f * AUTOSCREEN_HEIGHT];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.userText.text length])];
        self.userText.attributedText = attributedString;
        [attributedString release];
    }
    
    NSArray *imageArr = [self.recordMod.context componentsSeparatedByString:@"|"];
    NSURL *urlImageV = [NSURL URLWithString:[imageArr lastObject]];
    [self.userImageV sd_setImageWithURL:urlImageV placeholderImage:PICHOLDEER];
    
    
    
    self.activityName.text = self.recordMod.projectName;
    self.timeL.text = [self.recordMod.time stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    self.userImageV.frame = CGRectMake(self.userText.frame.origin.x, self.userText.frame.origin.y + self.userText.frame.size.height + 5 * AUTOSCREEN_HEIGHT, 200  * AUTOSCREEN_WIDTH, 140 * AUTOSCREEN_HEIGHT);
    
    
    
    self.sourceLabel.frame = CGRectMake(self.userImageV.frame.origin.x, self.userImageV.frame.origin.y + self.userImageV.frame.size.height + 10 * AUTOSCREEN_WIDTH, 50 * AUTOSCREEN_HEIGHT, 20 * AUTOSCREEN_HEIGHT);
    self.activityName.frame = CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.frame.size.width, self.sourceLabel.frame.origin.y, self.userName.frame.size.width - self.sourceLabel.frame.size.width, self.sourceLabel.frame.size.height);
    self.timeL.frame = CGRectMake(self.userName.frame.origin.x, self.activityName.frame.origin.y + self.activityName.frame.size.height + 5 * AUTOSCREEN_HEIGHT, self.userName.frame.size.width, self.activityName.frame.size.height);
    
    self.viewBack.frame = CGRectMake(WIDTH, WIDTH, SCREEN_WIDTH - WIDTH * 2, self.timeL.frame.size.height + self.timeL.frame.origin.y + 10 * AUTOSCREEN_HEIGHT);
    
}
//计数cell视图的高度
+ (CGFloat)getRecordViewHeight:(RecordMod *)recordMod {

    
    UIView *viewBack = [[UIView alloc] initWithFrame:CGRectMake(WIDTH, WIDTH, SCREEN_WIDTH - WIDTH * 2 * AUTOSCREEN_HEIGHT, (SCREEN_HEIGHT - 64 - 49) / 2.f - WIDTH)];
    
    
    UIImageView *userHeadImageV = [[UIImageView alloc] initWithFrame:CGRectMake(10  * AUTOSCREEN_WIDTH, 13 * AUTOSCREEN_HEIGHT, 45 * AUTOSCREEN_WIDTH, 45 * AUTOSCREEN_HEIGHT)];
    
    
    
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(userHeadImageV.frame.origin.x + userHeadImageV.frame.size.width + 10 * AUTOSCREEN_WIDTH, userHeadImageV.frame.origin.y - 5 * AUTOSCREEN_HEIGHT, viewBack.frame.size.width - (userHeadImageV.frame.origin.x + userHeadImageV.frame.size.width + 25 * AUTOSCREEN_WIDTH), userHeadImageV.frame.size.height * (2 / 3.f))];
    
    
    UILabel *userText = [[UILabel alloc] initWithFrame:CGRectMake(userName.frame.origin.x, userName.frame.origin.y + userName.frame.size.height + 2 * AUTOSCREEN_HEIGHT, userName.frame.size.width, userName.frame.size.height)];
    
    userText.text = recordMod.comment;
    
    
    CGRect textRect = userText.frame;
    textRect.size.height = [AppTools heightForContent:recordMod.comment width:userText.frame.size.width font:16.f * AUTOSCREEN_WIDTH]  * AUTOSCREEN_HEIGHT;
    userText.frame = textRect;
    
    if (0 != userText.text.length) {
        // 调整行间距
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:userText.text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3.f * AUTOSCREEN_HEIGHT];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [userText.text length])];
        userText.attributedText = attributedString;
        [attributedString release];
        
    }
    
    UIImageView *userImageV = [[UIImageView alloc] initWithFrame:CGRectMake(userText.frame.origin.x, userText.frame.origin.y + userText.frame.size.height + 5 * AUTOSCREEN_HEIGHT, 200 * AUTOSCREEN_WIDTH, 140 * AUTOSCREEN_HEIGHT)];
    
    
    
    UILabel *sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(userImageV.frame.origin.x, userImageV.frame.origin.y + userImageV.frame.size.height + 5 * AUTOSCREEN_HEIGHT, 50 * AUTOSCREEN_WIDTH, 20 * AUTOSCREEN_HEIGHT)];
    sourceLabel.text = @"来自于";
    //    sourceLabel.backgroundColor = [UIColor orangeColor];
    sourceLabel.font = [UIFont systemFontOfSize:14.f];
    
    
    
    
    UILabel *activityName = [[UILabel alloc] initWithFrame:CGRectMake(sourceLabel.frame.origin.x + sourceLabel.frame.size.width, sourceLabel.frame.origin.y, userName.frame.size.width - sourceLabel.frame.size.width, sourceLabel.frame.size.height)];
    
    
    
    UILabel *timeL = [[UILabel alloc] initWithFrame:CGRectMake(userName.frame.origin.x, activityName.frame.origin.y + activityName.frame.size.height + 5 * AUTOSCREEN_HEIGHT, userName.frame.size.width, activityName.frame.size.height)];
    
    viewBack.frame = CGRectMake(WIDTH, WIDTH, SCREEN_WIDTH - WIDTH * 2, timeL.frame.size.height + timeL.frame.origin.y + 10 * AUTOSCREEN_HEIGHT);
    

    
    CGFloat height = viewBack.frame.size.height + 12 * AUTOSCREEN_HEIGHT;

    [userName release];
    [userText release];
    [sourceLabel release];
    [activityName release];
    [timeL release];
    [viewBack release];
    [userHeadImageV release];
    
    return height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
